// Copyright 2011 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bridges.facebook {
	import bridges.storageservice.StorageServiceAccountBase;
	import bridges.storageservice.StorageServiceError;
	
	import events.LoginEvent;
	
	public class FacebookAccountBase extends StorageServiceAccountBase {

		override protected function Authorize(): void {

			var OnCallbackDone:Function = function(err:Number, strError:String): void {
				if (err == PicnikService.errNone) {
					AccountMgr.GetInstance().RefreshUserAttributes(function():void {
						var strUserId:String = AccountMgr.GetInstance().GetUserAttribute("tpa.Facebook._userid", "")
						var strToken:String = AccountMgr.GetInstance().GetUserAttribute("tpa.Facebook._token", "")
						_tpa.SetUserId(strUserId);
						_tpa.SetToken(strToken);
						_tpa.storageService.LogIn(_tpa, function(err:int, errMsg:String): void {
							dispatchEvent(new LoginEvent(LoginEvent.LOGIN_COMPLETE, (err==PicnikService.errNone)));
						});
					}, false); // false == don't save old attrs first
				}
			}

			var fnComplete:Function = function(err:Number, strError:String, strCBParams:String=null): void {
				if (err == StorageServiceError.None) {
					var fSuccess:Boolean = err == StorageServiceError.None;
					if (fSuccess) {
	
						var tpa:ThirdPartyAccount = AccountMgr.GetThirdPartyAccount("facebook");
						if (tpa && tpa.storageService) {
							try {
								var obParams:Object = {cbService:"facebook", cbParams:strCBParams, retriesleft:2};
								trace( "Authorize:calling user.callback" );
								PicnikService.callMethod("user.callback", null, obParams, true, null, OnCallbackDone); // Log errors
							} catch (e:Error) {
								trace(e);
							}
						}
					}
				}
			}
			
			ClearOtherConnections();
			_tpa.storageService.Authorize(null, fnComplete);
		}
	}
}
